package com.qd.push.service;

import cn.hutool.core.exceptions.ExceptionUtil;
import com.cdqidi.util.DateUtil;
import com.qd.common.panda.constant.PandaConstant;
import com.qd.common.panda.domain.entity.hk.HkParkOrderDTO;
import com.qd.hik.dto.HkBaseHttpResp;
import com.qd.hik.dto.charge.HkPayArrearsFeeReq;
import com.qd.hik.dto.charge.HkPayArrearsFeeResp;
import com.qd.hik.dto.charge.HkPaymentNoticeReq;
import com.qd.hik.service.HkChargeService;
import com.qd.push.model.HkPushLog;
import com.qd.push.model.HkPushReq;
import com.qd.push.model.HkPushResp;
import lombok.RequiredArgsConstructor;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

/**
 * @author sjk
 */
@Service
@RequiredArgsConstructor
@Slf4j
public class OperationService {
    private final HkChargeService hkChargeService;

    public void send(HkParkOrderDTO orderDTO, HkPushLog hkPushLog, HkPushReq hkPushReq, HkPushResp hkPushResp) {
        HkBaseHttpResp<?> hkBaseHttpResp = null;
        int sendState = 0;
        try {
            if (orderDTO.getOrderType().equals(PandaConstant.HK_ORDER_TYPE_1)) {
                //临停
                hkBaseHttpResp = paymentNotice(orderDTO);
            }
            if (orderDTO.getOrderType().equals(PandaConstant.HK_ORDER_TYPE_2)) {
                //欠费
                hkBaseHttpResp = payArrearsFee(orderDTO);
                Object data = hkBaseHttpResp.getData();
                if (null != data) {
                    try {
                        HkPayArrearsFeeResp resp = (HkPayArrearsFeeResp) data;
                        orderDTO.setBillCode(resp.getBillNo());
                    } catch (Exception ignored) {
                    }
                }
            }
        } catch (Throwable e) {
            log.error("send", e);
            hkPushResp.setRespParam(ExceptionUtil.getMessage(e));
            sendState = 1;
        } finally {
            String url = null;
            if (null != hkBaseHttpResp) {
                if (!hkBaseHttpResp.isSuccess()) {
                    hkPushResp.setRespParam("错误代码: " + hkBaseHttpResp.getCode() + ",错误信息: " + hkBaseHttpResp.getMsg());
                    sendState = 1;
                }
                if (null != hkPushReq) {
                    hkPushReq.setReqParam(hkBaseHttpResp.getReqJson());
                    url = hkBaseHttpResp.getReqUrl();
                }
                hkBaseHttpResp.freeData();
            }
            hkPushLog.setSendState(sendState);
            hkPushLog.setSendUrl(url);
        }
    }

    /**
     * 临停通知
     *
     * @param orderDTO 账单
     * @return 返回值
     */
    @SneakyThrows
    public HkBaseHttpResp<?> paymentNotice(HkParkOrderDTO orderDTO) {
        HkPaymentNoticeReq req = new HkPaymentNoticeReq();
        req.setBillNo(orderDTO.getBillCode());
        req.setThirdBillNo(orderDTO.getBid());
        req.setPayTime(DateUtil.localDateTimeToString(orderDTO.getPayTime(), "yyyyMMddHHmmss"));
        req.setPayType(3);
        req.setPayMoney(orderDTO.getPayMoney());
        if (null != orderDTO.getDeductMoney()) {
            req.setDeductMoney(orderDTO.getDeductMoney());
        }
        HkBaseHttpResp<?> hkBaseHttpResp = hkChargeService.paymentNotice(req);
        req.freeData();
        return hkBaseHttpResp;
    }

    /**
     * 欠费补交通知
     *
     * @param orderDTO 账单
     * @return 返回值
     */
    @SneakyThrows
    public HkBaseHttpResp<HkPayArrearsFeeResp> payArrearsFee(HkParkOrderDTO orderDTO) {
        HkPayArrearsFeeReq req = new HkPayArrearsFeeReq();
        req.setBillCode(orderDTO.getBillCode());
        req.setPayTime(System.currentTimeMillis() + "");
        req.setPayType(3);
        req.setThirdBillNo(orderDTO.getBid());
        if (null != orderDTO.getDeductMoney()) {
            req.setDeductMoney(orderDTO.getDeductMoney());
        }
        HkBaseHttpResp<HkPayArrearsFeeResp> hkBaseHttpResp = hkChargeService.payArrearsFee(req);
        req.freeData();
        return hkBaseHttpResp;
    }
}
